home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / libcairo-perl / examples / png / spline-pipeline.pl < prev    next >
Encoding:
Perl Script  |  2006-06-03  |  2.5 KB  |  130 lines

  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use Cairo;
  6.  
  7. use constant
  8. {
  9.     LINE_WIDTH => 13,
  10. };
  11.  
  12. sub spline_path
  13. {
  14.     my ($cr) = @_;
  15.  
  16.     $cr->save;
  17.     {
  18.         $cr->translate (-106.0988385, -235.84433);
  19.         $cr->move_to (49.517857, 235.84433);
  20.         $cr->curve_to (86.544809, 175.18401,
  21.                        130.19603, 301.40165,
  22.                        162.67982, 240.42946);
  23.     }
  24.     $cr->restore;
  25. }
  26.  
  27. sub source_path
  28. {
  29.     my ($cr) = @_;
  30.     spline_path ($cr);
  31.     $cr->set_line_width (1);
  32.     $cr->stroke;
  33. }
  34.  
  35. sub stroke
  36. {
  37.     my ($cr) = @_;
  38.     spline_path ($cr);
  39.     $cr->set_line_width (LINE_WIDTH);
  40.     $cr->stroke;
  41. }
  42.  
  43. sub scale_both_set_line_width_stroke
  44. {
  45.     my ($cr) = @_;
  46.     $cr->scale (0.5, 0.5);
  47.     spline_path ($cr);
  48.     $cr->set_line_width (LINE_WIDTH);
  49.     $cr->stroke;
  50. }
  51.  
  52. sub scale_both_set_line_width_double_stroke
  53. {
  54.     my ($cr) = @_;
  55.     $cr->scale (0.5, 0.5);
  56.     spline_path ($cr);
  57.     $cr->set_line_width (2 * LINE_WIDTH);
  58.     $cr->stroke;
  59. }
  60.  
  61. sub save_scale_path_restore_set_line_width_stroke
  62. {
  63.     my ($cr) = @_;
  64.     $cr->save;
  65.     {
  66.         $cr->scale (0.5, 1.0);
  67.         spline_path ($cr);
  68.     }
  69.     $cr->restore;
  70.  
  71.     $cr->set_line_width (LINE_WIDTH);
  72.     $cr->stroke;
  73. }
  74.  
  75. # XXX: Ouch. It looks like there's an API bug in the implemented semantics for
  76. # cairo_set_line_width. I believe the following function
  77. # (set_line_width_scale_path_stroke_BUGGY) should result in a figure identical
  78. # to the version above it (save_scale_path_restore_set_line_width_stroke), but
  79. # it's currently giving the same result as the one beloe
  80. # (scale_path_set_line_width_stroke).
  81. sub set_line_width_scale_path_stroke_BUGGY
  82. {
  83.     my ($cr) = @_;
  84.     $cr->set_line_width (LINE_WIDTH);
  85.     $cr->scale (0.5, 1.0);
  86.     spline_path ($cr);
  87.     $cr->stroke;
  88. }
  89.  
  90. sub scale_path_set_line_width_stroke
  91. {
  92.     my ($cr) = @_;
  93.     $cr->scale (0.5, 1.0);
  94.     $cr->set_line_width (LINE_WIDTH);
  95.     spline_path ($cr);
  96.     $cr->stroke;
  97. }
  98.  
  99. {
  100.     my @pipelines = (
  101.         \&source_path,
  102.         \&stroke,
  103.         \&scale_both_set_line_width_stroke,
  104.         \&scale_both_set_line_width_double_stroke,
  105.         \&save_scale_path_restore_set_line_width_stroke,
  106.         \&scale_path_set_line_width_stroke,
  107.     );
  108.     my $width = 140;
  109.     my $height = 68.833 * scalar @pipelines;
  110.  
  111.     my $surface = Cairo::ImageSurface->create ('argb32', $width, $height);
  112.     my $cr = Cairo::Context->create ($surface);
  113.  
  114.     foreach (0 .. $#pipelines) {
  115.         $cr->save;
  116.         {
  117.             $cr->translate ($width/2, ($_+0.5)*($height/scalar @pipelines));
  118.             $pipelines[$_]->($cr);
  119.         }
  120.         $cr->restore;
  121.         if ($cr->status ne 'success') {
  122.             warn "Cairo is unhappy after pipeline #$_: " . $cr->status . "\n";
  123.             exit 1;
  124.         }
  125.     }
  126.  
  127.     $surface->write_to_png ('spline-pipeline.png');
  128. }
  129.  
  130.